﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using myBoard.App.Components.MagnetsStore.ViewsModels;
using myBoard.App.Model;
using myBoard.Framework;
using myBoard.App.MagnetStore;

namespace myBoard.App.Components.MagnetsStore
{
    /// <summary>
    /// Interaction logic for MagnetScreen.xaml
    /// </summary>
    public partial class MagnetScreen : UserControl
    {
        public event EventHandler CloseRequested;
        private Storyboard _hideReviewsSB;

        public MagnetScreen()
        {
            InitializeComponent();
            _backBt.Click += new RoutedEventHandler(_backBt_Click);
            _magnetReviewsControl.ReturnToMagnetDetailsRequested += new MagnetReview.ReturnToMagnetDetailsRequestedHandler(_magnetReviewsControl_ReturnToMagnetDetailsRequested);
            _hideReviewsSB = this.Resources["HideReviews"] as Storyboard;
            _magnetReviewsControl.SubmitCommentRequested += new MagnetReview.SubmitCommentRequestedHandler(_magnetReviewsControl_SubmitCommentRequested);
            _reviewsBt.Click += new RoutedEventHandler(_reviewsBt_Click);
            _downloadProgresssBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(_downloadProgresssBar_ValueChanged);
        }

        void _downloadProgresssBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (e.NewValue == _downloadProgresssBar.Maximum || e.NewValue == 0)
                _downloadProgresssBar.Visibility = Visibility.Hidden;
            else
                _downloadProgresssBar.Visibility = Visibility.Visible;
        }

        void _reviewsBt_Click(object sender, RoutedEventArgs e)
        {
            RefreshMagnetComments();
        }

        void _magnetReviewsControl_SubmitCommentRequested(myBoard.App.MagnetStore.MagnetComment comment)
        {
            Instances instances = new Instances();
            string sessionId = instances.UsersViewModel.ConnectedUserSessionId;
            MagnetViewModel mvm = this.DataContext as MagnetViewModel;
            MagnetsStoreClient.Client.SubmitMagnetCommentCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_SubmitMagnetCommentCompleted);
            MagnetsStoreClient.Client.SubmitMagnetCommentAsync(sessionId, mvm._magnet.Id, comment.Title, comment.Description, comment.Rating);
        }

        void Client_SubmitMagnetCommentCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MagnetsStoreClient.Client.SubmitMagnetCommentCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_SubmitMagnetCommentCompleted);
            if (e.Error != null)
            {
                ExceptionsManager.LogException("Magnet Store", "Review submission failed", e.Error, System.Diagnostics.EventLogEntryType.Error, true);
                return;
            }
            myBoard.Framework.Forms.MessageBox.Show("Review submission succeded", "Thank you for reviewing this magnet !", "Magnet Store");
            RefreshMagnetComments();            
        }

        private void RefreshMagnetComments()
        {
            MagnetViewModel mvm = this.DataContext as MagnetViewModel;
            MagnetsStoreClient.Client.GetMagnetCommentsRangeCompleted += new EventHandler<GetMagnetCommentsRangeCompletedEventArgs>(Client_GetMagnetCommentsRangeCompleted);
            MagnetsStoreClient.Client.GetMagnetCommentsRangeAsync(mvm._magnet.Id, 0, 20);
        }

        void Client_GetMagnetCommentsRangeCompleted(object sender, GetMagnetCommentsRangeCompletedEventArgs e)
        {
            MagnetsStoreClient.Client.GetMagnetCommentsRangeCompleted -= new EventHandler<GetMagnetCommentsRangeCompletedEventArgs>(Client_GetMagnetCommentsRangeCompleted);
            if (e.Error != null)
            {
                ExceptionsManager.LogException("Magnets Store", "Failed to refresh current magnet reviews.", e.Error, System.Diagnostics.EventLogEntryType.Error, true);
                return;
            }
            MagnetViewModel mvm = this.DataContext as MagnetViewModel;
            mvm.MagnetCommentsViewModel = new System.Collections.ObjectModel.ObservableCollection<MagnetCommentViewModel>();
            foreach (MagnetComment magnetComment in e.Result)
            {
                mvm.MagnetCommentsViewModel.Add(new MagnetCommentViewModel(magnetComment));
            }            
        }

        private void RefreshCurrentMagnet()
        {
            MagnetsStoreClient.Client.GetMagnetCompleted += new EventHandler<GetMagnetCompletedEventArgs>(Client_GetMagnetCompleted);
            Instances instances = new Instances();
            MagnetViewModel mvm = this.DataContext as MagnetViewModel;
            MagnetsStoreClient.Client.GetMagnetAsync(instances.UsersViewModel.ConnectedUserSessionId, mvm._magnet.Id);
        }

        void Client_GetMagnetCompleted(object sender, GetMagnetCompletedEventArgs e)
        {
            MagnetsStoreClient.Client.GetMagnetCompleted -= new EventHandler<GetMagnetCompletedEventArgs>(Client_GetMagnetCompleted);
            if (e.Error != null)
            {
                ExceptionsManager.LogException("Magnets Store", "Failed to refresh current magnet info.", e.Error, System.Diagnostics.EventLogEntryType.Error, true);
                return;
            }
            Instances instances = new Instances();
            instances.MagnetsViewModel.SelectedMagnet = new MagnetViewModel(e.Result);
        }

        void _magnetReviewsControl_ReturnToMagnetDetailsRequested(MagnetReview sender)
        {
            _hideReviewsSB.Completed += new EventHandler(HideReviewsSB_Completed);            
            _hideReviewsSB.Begin();            
        }

        void HideReviewsSB_Completed(object sender, EventArgs e)
        {
            _hideReviewsSB.Completed -= new EventHandler(HideReviewsSB_Completed);
            RefreshCurrentMagnet();
        }

        void _backBt_Click(object sender, RoutedEventArgs e)
        {
            CloseRequested(sender, e);
        }
    }
}
